package org.ilovecode.display {
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import org.ilovecode.Utilities;
	
	/**
	 * ...
	 * @author Vjekoslav Ratkajec
	 */
	public class GalleryItem extends EventDispatcher {
		
		private var _name:String = "";
		private var _imagePath:String = "";
		private var _thumbPath:String = "";
		private var _thumb:Sprite = null;
		private var _image:Sprite = null;
		
		public function GalleryItem(name:String, imagePath:String, thumbPath:String) {
			_name = name;
			_imagePath = imagePath;
			_thumbPath = thumbPath;
			
			Utilities.loadContent("Content/Images/Galerija/" + thumbPath, loadThumbComplete);
			Utilities.loadContent("Content/Images/Galerija/" + imagePath, loadImageComplete);
		}
		
		private function loadThumbComplete(event:Event):void {
			_thumb = getContent(event);
			checkIfCompleted();
		}
		
		private function loadImageComplete(event:Event):void {
			_image = getContent(event);
			checkIfCompleted();
		}
		
		private function checkIfCompleted():void {
			if (isImageLoaded && isThumbLoaded) {
				this.dispatchEvent(new Event(Event.COMPLETE));
			}
		}
		
		private function getContent(event:Event):Sprite {
			var loader:Loader = Loader(event.currentTarget.loader);
			var content:DisplayObject = loader.content;
			var result:Sprite = new Sprite();
			result.addChild(content);
			return result;
		}
		
		//{ region Getters
		
		public function get name():String {
			return _name;
		}
		
		public function get imagePath():String {
			return _imagePath;
		}
		
		public function get thumbPath():String {
			return _thumbPath;
		}
		
		public function get thumb():Sprite {
			return _thumb;
		}
		
		public function get image():Sprite {
			return _image;
		}
		
		public function get isThumbLoaded():Boolean {
			if (_thumb != null) {
				return true;
			}
			return false;
		}
		
		public function get isImageLoaded():Boolean {
			if (_image != null) {
				return true;
			}
			return false;
		}
		
		//} endregion
		
	}

}